package com.li.controller;

import com.li.pojo.Result;
import com.li.pojo.User;
import com.li.service.UserService;
import com.li.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Map<String,Object> login(User user){
        Map<String,Object> userInfo = new HashMap<>();
        System.out.println("收到"+user);
        user = userService.login(user.getAccount(),user.getPassword());
        if (user!=null){
            user.setPassword("");
            userInfo.put("userInfo",user);
            userInfo.put("token",user.getUsername());
            userInfo.put("msg","登录成功");
        }else {
            userInfo.put("msg","登录失败");
        }
        System.out.println("发送用户"+user);
        return userInfo;

    }

    @RequestMapping("/getUsername")
    public String getUsername(HttpSession session){
        String username = (String) session.getAttribute("username");
        return username;
    }
    @PostMapping("/getAllUsers")
    public List<User> getAllUsers(@RequestParam(value = "uids") List<String> uids){
        System.out.println(uids);
        List<User> users =userService.selectAllUserById(uids);
        System.out.println(users);
        return users;
    }
}
